# -*- coding: utf-8 -*-
from django.db import models
from django.utils import simplejson
from django.db.models import Q
from django.contrib.auth.models import User
from django.template.defaultfilters import slugify
from django.contrib.contenttypes.models import ContentType
from django.contrib.contenttypes import generic
from django.db.models import signals
from django.core.exceptions import ObjectDoesNotExist
from django.core.files.storage import default_storage
from django.core.cache import cache
from django.conf import settings
from django.core.paginator import Paginator, EmptyPage, EmptyPage, InvalidPage
from customs.profile import Profile
from customs.fields import BigIntegerField, SpaceSeparatedIntegerField

import datetime
import os
import re
import time
import random
from urllib import quote
    
from dramas.main.settings import *

class SNSProfile(Profile):
    uid = models.CharField(max_length = 255, blank = True, db_index = True)
    origin = models.CharField(u"用户来源", max_length = 30, choices = USER_ORIGIN, blank = True)
    nickname = models.CharField(u"昵称", max_length = 255, blank = True)
    tinyurl = models.URLField(verify_exists = False, default = '', blank = True)
    headurl = models.URLField(verify_exists = False, default = '', blank = True)
    
    university = models.CharField(u"所在地", max_length = 255, blank = True)
    
    sex = models.SmallIntegerField(u"性别", default = 1, choices = SEX)
    zidou = models.BooleanField(u"紫豆", default = False)
    
    watched_series_ids = models.TextField(u"已观看视频ID", blank = True)
    
    info_updated = models.DateTimeField(u"信息更新", default=datetime.datetime.now)
    
#    watched_episode_ids = models.TextField(u"已观看视频ID", blank = True)
    
    @property
    def page(self):
        if self.origin == 'xiaonei':
            return u"http://xiaonei.com/profile.do?id=%s" % self.uid
        elif self.origin == '51':
            return u"http://home.51.com/%s" % self.uid
            
class Series(models.Model):
    name = models.CharField(u"连续剧名称", max_length = 255)
    xiaoli_category_id = models.IntegerField(u"xiaoliCategoryID", default = 0)
    xiaoli_id = models.IntegerField(u"xiaoliID", db_index = True)
#    watched_xn_user_ids = models.TextField(u"已观看者xnID", blank = True)
#    watched_51_user_ids = models.TextField(u"已观看者51ID", blank = True)
    views_count = models.IntegerField(u"观看人数", default = 0)
    
    def __unicode__(self):
        return self.name
        
    class Meta:
        verbose_name = u"剧集"
        verbose_name_plural = u"剧集"
    
class Episode(models.Model):
    series = models.ForeignKey(Series, verbose_name =u"剧集", related_name = 'episodes')
    xiaoli_category_id = models.IntegerField(u"xiaoliCategoryID", default = 0)
    xiaoli_series_id = models.IntegerField(u"xiaoli剧集ID", db_index = True)
    series_name = models.CharField(u"剧集名称", max_length = 255)
    xiaoli_slug = models.CharField(u"xiaoli ID", max_length = 25, db_index = True)
    name = models.CharField(u"视频名称", max_length = 255)
    watched_xn_user_ids = models.TextField(u"已观看者xnID", blank = True)
    watched_51_user_ids = models.TextField(u"已观看者51ID", blank = True)
    views_count = models.IntegerField(u"观看人数", default = 0)
    
    def add_watched(self, user):
        user_id = str(user.id)
        if user.origin == 'xiaonei':
            id_list = [id for id in self.watched_xn_user_ids.split() if id][:20]
            print id_list
            if user_id in id_list:id_list.remove(user_id)
            id_list.insert(0,user_id)
            print id_list
            self.watched_xn_user_ids = " ".join(id_list)
            
        elif user.origin == '51':
            id_list = [id for id in self.watched_51_user_ids.split() if id][:20]
            if user_id in id_list:id_list.remove(user_id)
            id_list.insert(0,user_id)
            self.watched_51_user_ids = " ".join(id_list)
        
        xiaoli_series_id = str(self.xiaoli_series_id)
        id_list = [id for id in user.watched_series_ids.split() if id][:30]
        if xiaoli_series_id in id_list:id_list.remove(xiaoli_series_id)
        id_list.insert(0,xiaoli_series_id)
        user.watched_series_ids = " ".join(id_list)
    
    def get_watched_user(self,user):
        if user.origin == 'xiaonei':
            id_list = [id for id in self.watched_xn_user_ids.split() if id]
        elif user.origin == '51':
            id_list = [id for id in self.watched_51_user_ids.split() if id]
            
        return User.objects.filter(id__in = id_list)
    
    def save(self, force_insert=False, force_update=False):
        self.xiaoli_category_id = self.series.xiaoli_category_id
        self.xiaoli_series_id = self.series.xiaoli_id
        self.series_name = self.series.name
        super(Episode, self).save(force_insert, force_update)
        
    def __unicode__(self):
        return self.name
        
    class Meta:
        verbose_name = u"视频"
        verbose_name_plural = u"视频"
        
class Comment(models.Model):
    user = models.ForeignKey(User, verbose_name = u"用户", related_name = 'comments')
    origin = models.CharField(u"用户来源", max_length = 30, choices = USER_ORIGIN, blank = True)
#    reply_to = models.ForeignKey('self', verbose_name = u"回复给", related_name = 'replies', null = True, blank = True)
#    reply_to_user = models.ForeignKey(User, verbose_name = u"回复给用户", related_name = 'replies', null = True, blank = True)
#    replies_count = models.IntegerField(u"回复数", default = 0)
    content = models.TextField(u"讨论内容")
    created = models.DateTimeField(u"创建时间", auto_now_add = True)
    series = models.ForeignKey(Series, verbose_name =u"评论剧集", related_name = 'comments', null = True, blank = True)
    episode = models.ForeignKey(Episode, verbose_name =u"评论视频", related_name = 'comments', null = True, blank = True)
    
    
#    def save(self, force_insert=False, force_update=False):
#        self.origin = self.user.origin
#        if self.reply_to:
#            self.reply_to_user = self.reply_to.user
#            self.reply_to.replies_count = self.reply_to.replies_count + 1
#            self.reply_to.save()
#        super(Comment, self).save(force_insert, force_update)
        
    def __unicode__(self):
        return u"%s: %s" % (self.user, self.content[:30])
        
    class Meta:
        verbose_name = u"评论"
        verbose_name_plural = u"评论"